package ScriptEngineDemo;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

public class ScriptEngineDemoMain {


    public static void main(String[] args)  {
      
      /*String num = "1";
      String num2 = "22";
      String max = js(num, num2, "max");
      String min= js(num, num2, "min");
      System.out.println(max);
      System.out.println(min);*/

      String sourceJsonStr = "{\"mobile\":\"18157125217\",\"lastTime\":\"1530598488000\",\"area\":\"浙江-杭州\",\"numberType\":\"中国电信 天翼\",\"chargesStatus\":\"1\",\"status\":\"1\"}";
      String formatScript = "if(str[\"mobile\"] != null && str[\"mobile\"] != undefined && str[\"mobile\"] != ''){str[\"mobile\"] = hideInfo(str[\"mobile\"],3,4);}";
      System.out.println(formatJson(sourceJsonStr,formatScript));


   }

   private static String formatJson(String sourceJsonStr,String formatScript){
       String back=null;
       ScriptEngineManager sem = new ScriptEngineManager();
       ScriptEngine se = sem.getEngineByName("javascript");
       try {
           String script = "function main(){function hideInfo(value,prefixLen,suffixLen){var star='*********************************';var totalLen=prefixLen+suffixLen;value+=\"\";if(value.length>totalLen){var prefix=value.substr(0,prefixLen);var suffix=value.substr(value.length-suffixLen);var middle=star.substr(0,value.length-totalLen);return prefix+middle+suffix}else if(value.length<=totalLen&&value.length>prefixLen){var prefix=value.substr(0,prefixLen);var middle=star.substr(0,value.length-prefixLen);return prefix+middle}else if(value.length<=prefixLen){return value}}var str=JSON.parse('%s');%s;return JSON.stringify(str)}";
           script = String.format(script,sourceJsonStr,formatScript);
           se.eval(script);
           Invocable inv = (Invocable) se;
           back=(String)inv.invokeFunction("main");
       } catch(Exception e) {
           System.out.println("java执行失败！");
           e.printStackTrace();

       }
       return back;
   }

   private static String js(String num,String num2, String type)  {

      String back=null;
      ScriptEngineManager sem = new ScriptEngineManager();
      ScriptEngine se = sem.getEngineByName("javascript");
       try {
           String script = "function test() {" +
                   "function max(n1,n2) {" +
                   "if(n1>n2){return n1}" +
                   "else {return n2}" +
                   "};" +
                   "function min(n1,n2) {" +
                   "if(n1<n2){return n1}" +
                   "else {return n2}" +
                   "};" +
                   "var flag = '" + type + "';" +
                   "if(flag == 'max'){return max('" + num + "','" + num2 + "')}" +
                   "else if(flag == 'min'){return min('" + num + "','" + num2 + "')}" +
                   "}";

           se.eval(script);
           Invocable inv = (Invocable) se;
           back = (String) inv.invokeFunction("test", num);
       } catch (Exception e) {
           System.out.println("java执行失败！");
           e.printStackTrace();

       }
      return back;
   }
}
